<?php 
class View {
    private $view_name;
    
    private $blocks = array();
    protected $data = array();
    
    
    public function __construct($view_name) {
        $view_name = trim($view_name);
        if (empty($view_name)) {
            tos_die("View name cannot be empty");
        }
        $this->view_name = $view_name;
    }
    
    
    public function get_view_name() {
        return $this->view_name;
    }
    
    
    public function add_block($block, $box_name = '') {
        if ($block instanceof View) {
            $block_name = $block->get_view_name();
            $box_name = trim($box_name);
            $box_name = (empty($box_name)) ? 'default' : $box_name;
            
            $this->blocks[$box_name][$block_name] = $block;
        } else {
            tos_die('Error view type');
        }
    }
    
    
    public function has_data($key) {
        return isset($this->data[$key]);
    }
    
    
    public function add_data($key , $value) {
        $this->data[$key] = $value;
    }
    
    
    public function get_data($key) {
        if (isset($this->data[$key])) {
            return $this->data[$key];
        } else {
            return false;
        }
    }
    
    
    public function merge_data($data) {
        $this->data = array_merge($this->data, $data);
    }
    
    
    public function write($key) {
        if (isset($this->data[$key])) {
            echo $this->data[$key];
        }
    }
    
    
    public function write_array($key) {
        if (isset($this->data[$key])) {
            $arr = $this->data[$key];
            if (is_array($arr)) {
                echo implode('', $arr);
            }
        }
    }
    
    
    public function display($out_type = 'html') {
        $out_type = trim($out_type);
        if (empty($out_type)) {
            $out_type = 'html';
        }
        
        $custom_theme_dir = ROOT_DIR . '/theme/' . tos_get_config('theme');
        if (is_dir($custom_theme_dir)) {
            $file = "{$custom_theme_dir}/{$this->view_name}.{$out_type}.php";
            if (file_exists($file)) {
                include $file;
                return true;
            }
        }
        
        $sys_theme_dir = ROOT_DIR . '/theme/sys';
        $file = "$sys_theme_dir/{$this->view_name}.{$out_type}.php";
        if (file_exists($file)) {
            include $file;
            return true;
        }
        
        tos_die("cannot find view {$this->view_name}.{$out_type} ");
        return false;
    }
    
    public function fetch($out_type = 'html') {
        ob_start();
        $this->display($out_type);
        $out = ob_get_contents();
        ob_end_clean();
        return $out;
    }
    
    
    public function has_box($box_name) {
        if (isset($this->blocks[$box_name]) && is_array($this->blocks[$box_name])) {
            return true;
        } else {
            return false;
        }
    }
    
    
    public function display_box($box_name = 'default', $out_type = 'html') {
        if ($this->has_box($box_name)) {
            foreach ($this->blocks[$box_name] as $block) {
                $block->display($out_type);
            }
        }
    }
    
    
}
?>